package com.woniu.humanresource.controller;


import com.woniu.humanresource.entity.Mobilize;
import com.woniu.humanresource.entity.MobilizeImg;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.MobilizeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class MobilizeController {
    @Value("${upload.addResourceLocation}")
    private String addResourceLocation;
    @Resource
    MobilizeService mobilizeService;
    //分页查询异动信息
    @PostMapping("/mobilize/{pageIndex}/{pageSize}")
    public ResponseText<PageBean<Mobilize>> getMobilizes(@PathVariable("pageIndex") int page,
                                                         @PathVariable("pageSize") int pageSize,
                                                         @RequestBody Mobilize mobilize){
        ResponseText<PageBean<Mobilize>> responseText=null;
        try {
            PageBean<Mobilize> pageBean = mobilizeService.selectCondition(mobilize, page, pageSize);
            responseText=new ResponseText<PageBean<Mobilize>>(pageBean,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //添加异动信息
    @PostMapping("/mobilize")
    public ResponseText addMobilize(@RequestBody Mobilize mobilize){
        ResponseText responseText=null;
        try {
            mobilizeService.insertMobilize(mobilize);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //修改异动信息
    @PutMapping("/mobilize")
    public ResponseText updateMobilize(@RequestBody Mobilize mobilize){
        ResponseText responseText=null;
        try {
            mobilizeService.updateMobilize(mobilize);
            responseText=new ResponseText<PageBean<Mobilize>>(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //删除异动信息
    @DeleteMapping("/mobilize/{id}")
    public ResponseText deleteMobilize(@PathVariable("id") int id){
        ResponseText responseText=null;
        try {
            mobilizeService.deleteMobilize(id);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    //    上传附件
    @RequestMapping("/uploadMobilize")
    public ResponseText upload(MultipartFile mobilize,@RequestParam("mid") String mid) throws IOException {
        MobilizeImg mobilizeImg = new MobilizeImg();
        try {
            DateFormat dateFormat=new SimpleDateFormat("yyyy.MM.dd");
            String dateStr = dateFormat.format(new Date());
            File file1 = new File(addResourceLocation+"mobilize."+dateStr);
            if(!file1.exists()){
                file1.mkdirs();
            }
            String path=addResourceLocation+"mobilize."+dateStr;
            String fileName=UUID.randomUUID()+mobilize.getOriginalFilename();
            File file = new File(path,fileName);
            mobilize.transferTo(file);
            String netPath="http://localhost:8080/images/mobilize."+dateStr+"/"+fileName;
//            地址存入数据库
            mobilizeImg.setMid(Integer.parseInt(mid));
            mobilizeImg.setImg(netPath);
            mobilizeService.insertMobilizeImg(mobilizeImg);
            return new ResponseText(netPath,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
    }
    //删除异动图片信息
    @DeleteMapping("/mobilizeImg/{id}")
    public ResponseText deleteMobilizeImg(@PathVariable("id") int id){
        ResponseText responseText=null;
        try {
            mobilizeService.deleteMobilizeImg(id);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

}
